<?php
namespace app\admin\controller;
use think\Db;
use app\admin\validate\KnowledgeValidate;
class KnowledgeController extends CommonController {


    /**
     * 获取知识库列表
     */
    public function getInfo(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        if($_POST['keyword']!=''){
            $keyword = $_POST['keyword'];
            $where['a.knowledge_name|u.user_username'] = array('like',"%$keyword%");
        }

        if($_POST['knowledge_type']!=''){//状态
            $where['a.knowledge_type'] = $_POST['knowledge_type'];
        }
        if($_POST['start']!=''){//时间
            $where['a.knowledge_time'] =array('>=',$_POST['start']);
        }elseif ($_POST['end']!=''){
            $where['a.knowledge_time'] =array('<=',$_POST['end']);
        }elseif ($_POST['start']!=''&&$_POST['end']!=''){
            $where['a.knowledge_time'] =array('between',array($_POST['start'],$_POST['end']));
        }

        $where['a.knowledge_team_id'] = session('loginSession')['team_id'];//团队ID;
        $res = Db::table('up_knowledge')->alias('a')
            ->join('up_u_user u','u.user_id = a.knowledge_publisher')
            ->where($where)
            ->limit($rows * ($page - 1), $rows)
            ->field('u.user_username,a.*')
            ->order('a.knowledge_time DESC')
            ->select();
        $total =Db::table('up_knowledge')->alias('a')
            ->join('up_u_user u','u.user_id = a.knowledge_publisher')
            ->where($where)
            ->count();
        $uid = session('loginSession')['user_id'];
        foreach ($res as $key=>&$vo){
            if($vo['knowledge_publisher']==$uid){//判断登录的用户和这个课程发布的用户是否是同一个人
                $vo['is_release'] = 1;//同一个人的话就是有发布和取消发布的权限
                $vo['is_delete'] = 1;//同一个人的话就是有删除权限
                $vo['is_update'] = 1;//同一个人的话就是有删除权限
            }else{
                $vo['is_release'] = 0;//发布的权限
                $vo['is_update'] = 0;//删除权限
                $vo['is_update'] =0;//同一个人的话就是有删除权限

            }
        }
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * 发布
     */
    public function Confirmation(){
        $cid = $_POST['cid'];
        if($cid!=''){
            $where['knowledge_id'] = $cid;
            $data['knowledge_status'] = 1;//审核通过
            $res =  Db::table('up_knowledge')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = "发布成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "发布失败";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
            return json_encode($returnData);
        }
    }

    /***
     * 取消发布
     */
    public function QxConfirmation(){
        $cid = $_POST['cid'];
        if($cid!=''){
            $where['knowledge_id'] = $cid;
            $data['knowledge_status'] = 2;//审核通过
            $res =  Db::table('up_knowledge')->where($where)->update($data);
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = "取消发布成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "取消发布失败";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
            return json_encode($returnData);
        }
    }

    /**
     * 删除此课程
     */
    public function delConfirmation(){
        $cid = $_POST['cid'];
        $result = Db::table('up_knowledge')->where('knowledge_id',$cid)->find();
        $uid = session('loginSession')['user_id'];
        if($uid!=$result['knowledge_publisher']){
            $returnData['status'] = 0;
            $returnData['msg'] = "不能删除不属于自己的知识库";
            return json_encode($returnData);
        }

        if($cid!=''){
            $where['knowledge_id'] = $cid;
            $res =  Db::table('up_knowledge')->where($where)->delete();
            if($res){
                $returnData['status'] = 1;
                $returnData['msg'] = "删除成功";
                return json_encode($returnData);
            }else{
                $returnData['status'] = 0;
                $returnData['msg'] = "删除失败";
                return json_encode($returnData);
            }
        }else{
            $returnData['status'] = 0;
            $returnData['msg'] = "传递参数错误";
            return json_encode($returnData);
        }
    }

    /**
     * 添加页面
     */
    public function addInfo(){
        $sh ='3';//发布权限
        $uid = session('loginSession')['user_id'];
        $whereMap['user_id'] = $uid;
        $user_id = Db::table('up_team')->where($whereMap)->field('user_id')->find();//查询这个团队的创建人

        if($user_id==''){//查询这个人在团队里面是否有发布权限
            $whereUp['team_id'] = session('loginSession')['team_id'];//团队ID;
            $whereUp['team_member_user_id'] = $uid;
            //查询这个人在这个团队权限
            $team_member_role = Db::table('up_team_members')->where($whereUp)->field('team_member_role')->find();

            $whereRole['team_role'] = $team_member_role['team_member_role'];
            $whereRole['team_id'] =session('loginSession')['team_id'];//团队ID;

            //查询这个在在这个群里面的权限
            $RoleArray = Db::table('up_team_role')->where($whereRole)->field('team_role_detail')->find();
            if (strpos($RoleArray['team_role_detail'],$sh)===false) {
                return 1;
            }else{
                return $this->fetch('knowledge/addInfo');
            }
        }else{
            return $this->fetch('knowledge/addInfo');
        }
    }

    /***
     * 添加功能
     */
    public function insertKnowledge(){

        $validata = new KnowledgeValidate();
        if(!$validata->check($_POST)) {
            $returnData['status'] = 0;
            $returnData['msg'] = $validata->getError();
            return json_encode($returnData);
        }
        $uid = session('loginSession')['user_id'];


        if($_POST['knowledge_type']==1&&$_POST['knowledge_mold']==1){
            $teamWhere['knowledge_team_id'] = session('loginSession')['team_id'];
            $teamWhere['knowledge_type'] = 1;
            $result = Db::table('up_knowledge')->where($teamWhere)->field('knowledge_id')->find();
            if($result['knowledge_id']!=''){
                $returnData['status'] = 0;
                $returnData['msg'] = "每个团队的Banner课程只能发布一个,你所在的团队已经发布了";
                return json_encode($returnData);
            }
        }

        if($_POST['knowledge_type']!=3){
            if($_FILES['file']['name']=='') {
                $returnData['status'] = 0;
                $returnData['msg'] = "上传文件格式不能为空";
                return json_encode($returnData);
            }
            $rand = date('YmdHis').rand(100000,999999);
            $exe = explode('.',$_FILES['file']['name']);
            $filePath = $_FILES['file']['tmp_name'];
            $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
            if($info['status']!=200) {
                $returnData['status'] = 0;
                $returnData['msg'] = "上传文件格式不正确";
                return json_encode($returnData);
            }else{
                $data['knowledge_banner'] = QiNiu.$info['key'];//图片存储路径
            }
        }
        $data['knowledge_id'] = $this->getRandChar(32);
        $data['knowledge_context'] = $_POST['knowledge_context'];
        $data['knowledge_name'] =  $_POST['knowledge_name'];
        $data['knowledge_status'] = 1;
        $data['knowledge_time'] =date('Y-m-d H:i:s');
        $data['knowledge_team_id'] = session('loginSession')['team_id'];//团队ID;
        $data['knowledge_publisher'] = $uid;
        $data['knowledge_type'] = $_POST['knowledge_type'];
        $data['knowledge_mold'] = $_POST['knowledge_mold'];
        $res = Db::table('up_knowledge')->insert($data);
        if($res){
            $team_id = session('loginSession')['team_id'];//团队ID

            $team_name = session('loginSession')['team_name'];//团队名称
            if($_POST['knowledge_mold']==1){
                $where1['team_id'] = $team_id;
                $where1['team_member_remove'] = 2;
                $team_member_user_id = Db::table('up_team_members')->where($where1)->column('team_member_user_id');
                if(count($team_member_user_id)>0){
                    if($_POST['knowledge_type']==2){
                        $msg=$team_name.'团队发布了新的资讯，一睹为快!';
                        $msg1 = "{\"msg\":\"20\"}";
                        $msg_data = array('msg'=>$msg,'code'=>20,'fid'=>$data['knowledge_id']);
                        $code=20;
                        $this->sendJpush(1,$msg,$msg_data,$msg1,$team_member_user_id,$code);
                    }elseif ($_POST['knowledge_type']==3){
                        $msg1 = "{\"msg\":\"21\"}";
                        $msg=$team_name.'团队发布了新的技术资料，快去看看!';
                        $msg_data = array('msg'=>$msg,'code'=>21,'fid'=>$data['knowledge_id']);
                        $code=21;
                        $this->sendJpush(1,$msg,$msg_data,$msg1,$team_member_user_id,$code);
                    }
                }
            }else{
                $where1['team_member_remove'] = 2;
                $team_member_user_id = Db::table('up_team_members')->where($where1)->column('team_member_user_id');
                if(count($team_member_user_id)>0){
                    if($_POST['knowledge_type']==2){
                        $msg=$team_name.'团队发布了新的资讯，一睹为快!';
                        $msg1 = "{\"msg\":\"20\"}";
                        $msg_data = array('msg'=>$msg,'code'=>20,'fid'=>$data['knowledge_id']);
                        $code=20;
                        $this->sendJpush(1,$msg,$msg_data,$msg1,$team_member_user_id,$code);
                    }elseif ($_POST['knowledge_type']==3){
                        $msg1 = "{\"msg\":\"21\"}";
                        $msg=$team_name.'团队发布了新的技术资料，快去看看!';
                        $msg_data = array('msg'=>$msg,'code'=>21,'fid'=>$data['knowledge_id']);
                        $code=21;
                        $this->sendJpush(1,$msg,$msg_data,$msg1,$team_member_user_id,$code);
                    }
                }
            }

            return json_encode(array("status" => 1, "msg" => "发布成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "发布失败！"));
        }
    }

    /*
     * 查看详情
     */
    public function courseInfo(){
        $cid = $_POST['cid'];
        $res = Db::table('up_knowledge')->where('knowledge_id',$cid)->find();
        $this->assign('res',$res);
        return $this->fetch('knowledge/courseInfo');
    }

    /**
     * 编辑页面
     */
    public function updateKnowledge(){
        $uid = session('loginSession')['user_id'];

        $cid = $_POST['nid'];
        $res = Db::table('up_knowledge')->where('knowledge_id',$cid)->find();
        $this->assign('res',$res);
        if($res['knowledge_publisher']==$uid){
            return $this->fetch('knowledge/updateKnowledge');

        }else{
            return 1;
        }
    }

    public function updateInfo(){
        $where['knowledge_id'] = $_POST['knowledge_id'];
        $validata = new KnowledgeValidate();
        if(!$validata->check($_POST)) {
            $returnData['status'] = 0;
            $returnData['msg'] = $validata->getError();
            return json_encode($returnData);
        }
        $result = Db::table('up_knowledge')->where($where)->find();
        if($_POST['knowledge_type']!=3){
            if($_POST['knowledge_type']==1&&$_POST['knowledge_mold']==1){
                $teamWhere['knowledge_team_id'] = session('loginSession')['team_id'];
                $teamWhere['knowledge_type'] = 1;
                $teamWhere['knowledge_id'] = array('NEQ',$_POST['knowledge_id']);
                $result = Db::table('up_knowledge')->where($teamWhere)->field('knowledge_id')->find();
                if($result['knowledge_id']!=''){
                    $returnData['status'] = 0;
                    $returnData['msg'] = "每个团队的Banner课程只能发布一个,你所在的团队已经发布了";
                    return json_encode($returnData);
                }
            }
            if(!empty($_FILES['file']['type'])){
                $rand = date('YmdHis').rand(100000,999999);
                $exe = explode('.',$_FILES['file']['name']);
                $filePath = $_FILES['file']['tmp_name'];
                $info = $this->qnCommon(2,$rand,$exe[1],$filePath);
                if($info['status']!=200) {
                    $returnData['status'] = 0;
                    $returnData['msg'] = "上传文件格式不正确";
                    return json_encode($returnData);
                }else{
                    $data['knowledge_banner'] = QiNiu.$info['key'];;//图片存储路径
                }
            }
        }else{
            if($result['knowledge_type']!=3){
                $data['knowledge_banner'] ='';//图片存储路径
            }
        }

        $data['knowledge_context'] = $_POST['knowledge_context'];
        $data['knowledge_name'] =  $_POST['knowledge_name'];
        $data['knowledge_type'] = $_POST['knowledge_type'];
        $data['knowledge_mold'] = $_POST['knowledge_mold'];
        $res = Db::table('up_knowledge')->where($where)->update($data);
        if($res){
            return json_encode(array("status" => 1, "msg" => "发布成功！"));
        }else{
            return json_encode(array("status" => 0, "msg" => "发布失败！"));
        }
    }
}